package com.kurumi.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kurumi.enums.EnumQuery;
import com.kurumi.enums.EnumQueryDoctorDesc;
import com.kurumi.enums.EnumQueryICD;
import com.kurumi.enums.EnumQueryOther;
import com.kurumi.pojo.MedicalRecord;
import com.kurumi.pojo.RespondResult;
import com.kurumi.query.InPatientMedicalRecordQuery;
import com.kurumi.query.MedicalRecordSearchingQuery;
import com.kurumi.service.MedicalRecordCodingService;
import com.kurumi.service.SolrService;
import com.kurumi.solr.MedicalRecordSolr;
import com.kurumi.util.DateUtil;
import com.kurumi.util.ExcelUtils;

@Controller
@RequestMapping("/medical_record_searching")
public class MedicalRecordSearchingController {

	@Autowired
	private MedicalRecordCodingService medicalRecordCodingService;
	
	@Autowired
	private SolrService solrService;
	
	@GetMapping("/medical_record_searching_index")
	public String medicalRecordSearchingIndex(){
		return "medical_record_searching_index.default";
	}
	
	@GetMapping("/disease_diag_searching_page")
	public String diseaseDiagSearchingPage(){
		return "searching/disease_diag_searching_page";
	}
	
	@GetMapping("/custom_searching_page")
	public String customSearchingPage(Model model){
		
		EnumQueryOther[] queryObjs = EnumQueryOther.values();
		model.addAttribute("queryObjs",queryObjs);
		return "searching/custom_searching_page";
	}
	
	@GetMapping("/icd_searching_page")
	public String icdSearchingPage(Model model){
		
		EnumQueryICD[] queryObjs = EnumQueryICD.values();
		model.addAttribute("queryObjs",queryObjs);
		return "searching/icd_searching_page";
	}
	
	@GetMapping("/doctor_desc_searching_page")
	public String doctorDescSearchingPage(Model model){
		
		EnumQueryDoctorDesc[] queryObjs = EnumQueryDoctorDesc.values();
		model.addAttribute("queryObjs",queryObjs);
		return "searching/doctor_desc_searching_page";
	}
	
	
	@GetMapping("/disease_diag_searching")
	@ResponseBody
	public RespondResult diseaseDiagSearching(MedicalRecordSearchingQuery params){
		RespondResult respondResult = null;
		
		try{
			int count = 0;
			if(params.getDiseaseDiagName() != null){
				params.setDiseaseDiagName("%"+params.getDiseaseDiagName()+"%");
			}
			List<Map<String, Object>> diseaseMedicalRecords = medicalRecordCodingService.getDiseaseMedicalRecord(params);
			count = medicalRecordCodingService.getDiseaseMedicalRecordCount(params);
			params.setQueryDatas(diseaseMedicalRecords);
			params.setTotalCounts(count);
			respondResult = new RespondResult(true, RespondResult.successCode, null, params);
		}catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), null);
		}
		
		return respondResult;
	}
	
	@GetMapping("/ajax_custom_searching")
	@ResponseBody
	public RespondResult ajaxCustomSearching(InPatientMedicalRecordQuery query){
		RespondResult respondResult = null;
		
		query.clearEmpty();
		List<MedicalRecordSolr> datas = new ArrayList<MedicalRecordSolr>();
		try {
			if(!query.getItems().isEmpty()){
				datas = solrService.query(query);
				query.setDatas(datas);
			}
			respondResult = new RespondResult(true, RespondResult.successCode, null, query);
		} catch (Exception e) {
			// TODO: handle exception
			respondResult = new RespondResult(false, RespondResult.errorCode, e.getMessage(), null);
		}
		
		return respondResult;
	}
	
	@GetMapping("/searching_index")
	public String searchingIndex(){
		return "searching_index.search";
	}
	
	@GetMapping("/custom_searching_export_excel")
	public void customSearchingExportExcel(InPatientMedicalRecordQuery params,HttpServletResponse response){
		OutputStream out = null;
		try{
			params.setPageSize(null);
			/*
			medicalRecords = medicalRecordDRGsService.getCodingExport(params);*/
			response.setContentType("applicationnd.ms-excel");
			String title= "查询检索导出"+DateUtil.dateFormat(new Date());
			StringBuilder downLoadFileName = new StringBuilder();
			downLoadFileName = new StringBuilder(
					"attachment;filename=");
			downLoadFileName.append(title)
					.append(".xls");
			/*downLoadFileName.append(title).append(DateUtil.dateFormat(new Date()))
			.append(".dbf");*/
			String encodeStr = new String(downLoadFileName.toString().getBytes(
					"GB2312"), "ISO_8859_1");
			response.addHeader("Content-Disposition", encodeStr);
			out = response.getOutputStream();
			List<String> cols = new ArrayList<String>();
			params.clearEmpty();
			List<Map<String, String>> datas = solrService.queryExportExcel(params, cols);
			
			List<String> headers = new ArrayList<String>();
			List<String> keys = new ArrayList<String>();
			for (String col : cols) {
				headers.add(EnumQuery.getEnumByCode(col).getName());
				keys.add(col);
			}
			ExcelUtils.exportExcel1(out,headers, keys, datas);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
